package com.lanhu.designpattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * author:zhangzg
 * date:2020/11/1 2:58
 * 容器类单例模式
 */
public class SingletonContainer {

    private static Map<String, Object> container = new ConcurrentHashMap<>();

    private SingletonContainer (){}

    public static Object getBean(String className){
        synchronized (container){
            if (container.containsKey(className)){
                return container.get(className);
            }
            Object obj = null;
            try {
                obj = Class.forName(className).newInstance();
                container.put(className, obj);
                return obj;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
